//
//  ViewController.m
//  汽车品牌展示2
//
//  Created by 伍银荣 on 2021/6/3.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "ViewController.h"
#import "GroupModel.h"
#import "CarsModel.h"

@interface ViewController ()<UITableViewDataSource>

@property (nonatomic, strong) NSArray *groupArr;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

-(NSArray *)groupArr {
    if (!_groupArr) {
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            GroupModel *model = [GroupModel groupWithDictionary:dict];
            [mArr addObject:model];
        }
        _groupArr = mArr;
    }
    return _groupArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   // GroupModel *model = self.groupArr[section];
   // return model.cars.count;
    return [[self.groupArr[section] cars] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GroupModel *model = self.groupArr[indexPath.section];
    CarsModel *cars = model.cars[indexPath.row];
    static NSString *cellID = @"carsID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.imageView.image = [UIImage imageNamed:cars.icon];
    cell.textLabel.text = cars.name;
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.groupArr[section] title];
}

//设置tableView右侧的索引栏
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//    NSMutableArray *mArr = [NSMutableArray array];
//    for (GroupModel *model in self.groupArr) {
//        [mArr addObject:model.title];
//    }
//    return mArr;
    return [self.groupArr valueForKeyPath:@"title"];
}



@end
